using Engine;
using Game;

namespace SCIENEW {
    public class IERecipaediaScreen : RecipaediaScreen {
        public override void Update() {
            if (m_listCategoryIndex != m_categoryIndex) {
                PopulateBlocksList();
            }
            string arg = m_categories[m_categoryIndex] == null ? LanguageControl.Get("BlocksManager", "All Blocks") : LanguageControl.Get("BlocksManager", m_categories[m_categoryIndex]);
            m_categoryLabel.Text = $"{arg} ({m_blocksList.Items.Count})";
            m_prevCategoryButton.IsEnabled = m_categoryIndex > 0;
            m_nextCategoryButton.IsEnabled = m_categoryIndex < m_categories.Count - 1;
            int? value = null;
            int num = 0;
            if (m_blocksList.SelectedItem is int) {
                //搜寻合成配方
                value = (int)m_blocksList.SelectedItem;
                num = SearchForRecipe((int)value);
            }
            if (num > 0) {
                m_recipesButton.Text = $"{num} {((num == 1) ? LanguageControl.Get(nameof(RecipaediaScreen), 1) : LanguageControl.Get(nameof(RecipaediaScreen), 2))}";
                m_recipesButton.IsEnabled = true;
            }
            else {
                m_recipesButton.Text = LanguageControl.Get(nameof(RecipaediaScreen), 3);
                m_recipesButton.IsEnabled = false;
            }
            m_detailsButton.IsEnabled = value.HasValue;
            if (m_prevCategoryButton.IsClicked
                || Input.Left) {
                m_categoryIndex = MathUtils.Max(m_categoryIndex - 1, 0);
            }
            if (m_nextCategoryButton.IsClicked
                || Input.Right) {
                m_categoryIndex = MathUtils.Min(m_categoryIndex + 1, m_categories.Count - 1);
            }
            if (value.HasValue
                && m_detailsButton.IsClicked) {
                ScreensManager.SwitchScreen("RecipaediaDescription", value.Value, m_blocksList.Items.Cast<int>().ToList());
            }
            if (value.HasValue
                && m_recipesButton.IsClicked) {
                ScreensManager.SwitchScreen("RecipaediaRecipes", value.Value);
            }
            if (Input.Back
                || Input.Cancel
                || Children.Find<ButtonWidget>("TopBar.Back").IsClicked) {
                ScreensManager.SwitchScreen(m_previousScreen);
            }
        }

        public static int SearchForRecipe(int value) {
            int count = 0;
            //先遍历热量需求为分离器的配方
            foreach (var craftingRecipe in CraftingRecipesManager.Recipes.Where(x => x.RequiredHeatLevel == ComponentSifter.RequiredHeatLevel)) {
                string ingredient = IECraftingRecipesManager.BlockToIngredient(value);
                foreach (var requireIngredient in craftingRecipe.Ingredients) {
                    if (requireIngredient == null) continue;
                    if (IECraftingRecipesManager.CompareIngredients(requireIngredient, ingredient)) {
                        count++;
                        break;
                    }
                }
            }
            //再遍历原版的
            count += CraftingRecipesManager.Recipes.Count((CraftingRecipe r) => r.ResultValue == value && r.RequiredHeatLevel != ComponentSifter.RequiredHeatLevel);

            return count;
        }
    }
}